var msgInvalidPassword={'zh-CN':'密码格式不正确','en':'Invalid password'};
var msgResend={'zh-CN':'重新发送','en':'Send again'}; 
var msgInvalidMobile={'zh-CN':'手机号格式不正确','en':'Invalid mobile number'}; 
var msgInvalidEmail={'zh-CN':'邮箱格式不正确','en':'Invalid e-mail'}; 
var msgNoEmpty={'zh-CN':'不能为空','en':'Must fill in it!'}; 
var msgInvalidPwdRepeat={'zh-CN':'两次输入的密码不一致','en':'Password not matching'}; 
var msgInvalidTruename={'zh-CN':'真实姓名格式不对，必须是2~5个汉字','en':'Invalid real name'}; 
var msgInvalidid_card={'zh-CN':'身份证格式不对','en':'Invalid ID number'}; 
var msgTeenager={'zh-CN':'身份证号年龄小于18岁，将被纳入防沉迷系统。','en':'You will be protected by Anti-addiction System since your age is below 18'}; 
var msgNoAccept={'zh-CN':'请先阅读协议。','en':'Please read the agreement'}; 
var msgSuccessEmail={'zh-CN':'邮箱可以使用','en':'Account available'}; 
var msgNoEmail={'zh-CN':'邮箱已经注册','en':'User already existed!'}; 
var msgSuccessVcode={'zh-CN':'验证码正确','en':'Identifying code OK!'}; 
var msgNoVcode={'zh-CN':'验证码错误','en':'Identifying code error!'}; 

function goIndex(toUrl) {
	setTimeout(function() {
		location.href = toUrl ? toUrl:"/";
	}, 3000);
}
function setCookie(name, value) {
	document.cookie = name + "=" + value + "; path=/";
}
function getCookie(name) {
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {

		var c = $.trim(ca[i]).split('=');
		if (c.length == 2 && c[0] == name) {
			return c[1];
		}
	}
	return "";
}
function getLocale() {
	var locale = getCookie("locale");
	return 'en' == locale ? 'en' : 'zh-CN';
}
function setLocale(locale) {
	setCookie("locale", 'en' == locale ? 'en' : 'zh-CN');
}

function checkPassword(s) {
	if (is.empty(s)) {
		return false;
	}
	var patrn = /^[a-zA-Z0-9]{6,16}$/;
	return patrn.exec(s);
}
function getPasswordLevel(s) {
	if (s.length < 9) {
		return 0;
	} else if (s.length < 14) {
		return 1;
	} else {
		return 2;
	}
}
function checkUserName(username) {
	if (is.empty(username)) {
		return false;
	} else if (checkCellPhone(username) || is.email(username)) {
		return true;
	} else {
		return false;
	}
}

function checkTruename(str) {
	var pattern = /^[\u4E00-\u9FA5]{2,5}$/i;
	if (pattern.test(str)) {
		return true;
	} else
		return false;
}
function per15To18(perIDSrc) {
	var iS = 0;
	var iW = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
	var LastCode = "10X98765432";
	var perIDNew;

	perIDNew = perIDSrc.substring(0, 6);
	perIDNew += "19";

	perIDNew += perIDSrc.substring(6, perIDSrc.length);
	for (var i = 0; i < 17; i++) {
		iS += parseInt(perIDNew.substring(i, i + 1)) * iW[i];
	}

	var iY = iS % 11;
	perIDNew += LastCode.substring(iY, iY + 1);
	return perIDNew;
}
function checkid_card(idcardx) {
	var area = {
		11 : "北京",
		12 : "天津",
		13 : "河北",
		14 : "山西",
		15 : "内蒙古",
		21 : "辽宁",
		22 : "吉林",
		23 : "黑龙江",
		31 : "上海",
		32 : "江苏",
		33 : "浙江",
		34 : "安徽",
		35 : "福建",
		36 : "江西",
		37 : "山东",
		41 : "河南",
		42 : "湖北",
		43 : "湖南",
		44 : "广东",
		45 : "广西",
		46 : "海南",
		50 : "重庆",
		51 : "四川",
		52 : "贵州",
		53 : "云南",
		54 : "西藏",
		61 : "陕西",
		62 : "甘肃",
		63 : "青海",
		64 : "宁夏",
		65 : "新疆",
		71 : "台湾",
		81 : "香港",
		82 : "澳门",
		91 : "国外"
	}

	var idcard = idcardx.toUpperCase();
	var Y, JYM;
	var S, M;
	var idcard_array = new Array();
	idcard_array = idcard.split("");
	// 地区检验
	if (area[parseInt(idcard.substr(0, 2))] == null)
		return 4;
	// 身份号码位数及格式检验
	switch (idcard.length) {
	case 15:
		if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0
				|| ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard
						.substr(6, 2)) + 1900) % 4 == 0)) {
			ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;// 测试出生日期的合法性
		} else {
			ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;// 测试出生日期的合法性
		}
		if (ereg.test(idcard))
			return 0;
		else
			return 2;
		break;
	case 18:
		// 18位身份号码检测
		// 出生日期的合法性检查
		// 闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
		// 平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
		if (parseInt(idcard.substr(6, 4)) % 4 == 0
				|| (parseInt(idcard.substr(6, 4)) % 100 == 0 && parseInt(idcard
						.substr(6, 4)) % 4 == 0)) {
			ereg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;// 闰年出生日期的合法性正则表达式
		} else {
			ereg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;// 平年出生日期的合法性正则表达式
		}
		if (ereg.test(idcard)) {// 测试出生日期的合法性
			// 计算校验位
			S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
					+ (parseInt(idcard_array[1]) + parseInt(idcard_array[11]))
					* 9
					+ (parseInt(idcard_array[2]) + parseInt(idcard_array[12]))
					* 10
					+ (parseInt(idcard_array[3]) + parseInt(idcard_array[13]))
					* 5
					+ (parseInt(idcard_array[4]) + parseInt(idcard_array[14]))
					* 8
					+ (parseInt(idcard_array[5]) + parseInt(idcard_array[15]))
					* 4
					+ (parseInt(idcard_array[6]) + parseInt(idcard_array[16]))
					* 2 + parseInt(idcard_array[7]) * 1
					+ parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9])
					* 3;
			Y = S % 11;
			M = "F";
			JYM = "10X98765432";
			M = JYM.substr(Y, 1);// 判断校验位
			if (M == idcard_array[17])
				return 0; // 检测ID的校验位
			else
				return 3;
		} else
			return 2;
		break;
	default:
		return 1;
		break;
	}
}

function isTeenager(temp) {
	if (temp.length == 15) {
		temp = per15To18(temp);
	}
	var year = Number(temp.substring(6, 10)) + 18;
	var month = Number(temp.substring(10, 12)) - 1;
	var day = Number(temp.substring(12, 14));

	var myDate = new Date();
	myDate.setFullYear(year, month, day);
	var today = new Date();
	if (myDate > today) {
		return true;
	} else {
		return false;
	}
}

function checkRand(rand, callback) {
	$.post("/ajax/checkrand", {
		rand : rand
	}, function(data) {
		callback(data.code == 0);
	}, "json")
}

function checkCellPhone(s) {
	var patrn = /^1[0-9]{10}$/;
	if (patrn.exec(s)) {
		return true;
	} else {
		return false;
	}
}
function SendInterval(node, text) {
	this.date = 0;
	this.interval = null;
	this.text = msgResend[getLocale()];
	this.node = node;
	if (text != undefined)
		this.text = text;
}
SendInterval.prototype.sendProcess = function() {
	var node = this.node;
	var cur = new Date().getTime();
	var keep = (cur - this.date) / 1000;
	if (keep >= 60 && this.interval != null) {
		this.stop();
	} else {
		var remain = Math.round(60 - keep);
		this.setVal(this.text + '(' + remain + 's)');
	}
};
SendInterval.prototype.start = function() {
	var _self = this;
	this.date = new Date().getTime();
	this.interval = window.setInterval(function() {
		_self.sendProcess();
	}, 1000);
};
SendInterval.prototype.stop = function() {
	var node = this.node;
	window.clearInterval(this.interval);
	node.removeClass('btn_disable');
	this.setVal(this.text);
};
SendInterval.prototype.setVal = function(text) {
	if (this.node.is('input')) {
		this.node.val(text);
	} else {
		this.node.text(text);
	}
};
jQuery.fn.extend({
	sendCode : function(url, data, callback) {
		var btnSend = this;
		if (arguments[3] != undefined) {
			btnSend = arguments[3];
		}
		if (btnSend.hasClass('btn_disable'))
			return;
		var text = btnSend.attr('text');
		var sendInterval = new SendInterval(btnSend, text);
		btnSend.addClass('btn_disable');
		$.get(url, data, function(d) {
			callback(d);
			if (d.code == 0) {
				btnSend.addClass('btn_disable');
				sendInterval.start();
			} else {
				btnSend.removeClass('btn_disable');
			}
		}, 'json');

	},
	showCueMessage : function(message, isSuccess) {
		var cueNode = this.nextAll('.ihint');
		if (is.empty(message)) {
			cueNode.html('');
		} else if (is.space(message)) {
			cueNode.html('&nbsp;');
		} else {
			cueNode.text(message);
		}
		if (isSuccess) {
			cueNode.addClass('ihint_pass');
			cueNode.removeClass('ihint_error');
			this.closest('.ibox').removeClass('error');
		} else {
			cueNode.addClass('ihint_error');
			cueNode.removeClass('ihint_pass');
			this.closest('.ibox').addClass('error');
		}
		cueNode.show();
	},
	hasError : function() {
		var cueNode = this.nextAll('.ihint');
		return cueNode.hasClass('ihint_error');
	},
	passwordLevel : function() {
		var pwd = this.val();
		var level = this.closest('form').find('.pwLv');
		level.find('li').removeClass('cur');
		var i = getPasswordLevel(pwd);
		level.find('li').eq(i).addClass('cur');
		level.show();
		if (checkPassword(pwd)) {
			this.showCueMessage('', true);
		} else {
			this.showCueMessage(msgInvalidPassword[getLocale()], false);
		}
		this.nextAll('.levelcue_before').hide();
	}
});

$(function() {
	// 中英文切换
	$(".as_register").tabOn({
		menu : ".lan_tab a",
		box : ".lan_tabpage",
		page : getLocale() == 'en' ? 1 : 0
	});
	$('.lan_tab a').click(function() {
		var locale = $(this).attr('locale');
		setLocale(locale);
	});
});

(function () {
    var m = window.location.href.toString().match(new RegExp("\\?(?:.+&)?mtxvt=(.*?)(?:&.*)?$","i"));
    if(m && /^[0-9a-zA-Z]*$/g.test(m[1])){ var D = new Date (); D.setTime(D.getTime() + 10800000); document.cookie = "__mtxvt=" + escape(m[1]) + ";domain=.wanmei.com;path=/; expires=" + D.toGMTString();}
    var Z = window.location.href.toString().match(new RegExp("\\?(?:.+&)?wmrid=(.*?)(?:&.*)?$","i"));
    if(Z && /^[0-9a-zA-Z]*$/g.test(Z[1])){ var D = new Date (); D.setTime(D.getTime() + 10800000); document.cookie = "WMRID=" + escape(Z[1]) + ";domain=.wanmei.com;path=/; expires=" + D.toGMTString(); }
})()